{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "a34569d6",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 佛如：百度密钥\n",
    "API_KEY = 'xhW5WqhcgXwUwZmbwjKS2wik'\n",
    "SECRET_KEY = 'Yq5GPewY7YVVjmvaoWM2TvGFKWByiiTn'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "0d9ee51b",
   "metadata": {},
   "outputs": [],
   "source": [
    "from bdasr import fetch_token,asr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "843d307d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "SUCCESS WITH TOKEN: 24.1942892820a687c76db5af50049c8a45.2592000.1655346116.282335-26194903 ; EXPIRES IN SECONDS: 2592000\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'24.1942892820a687c76db5af50049c8a45.2592000.1655346116.282335-26194903'"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ru_token = fetch_token(API_KEY,SECRET_KEY)\n",
    "ru_token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "f0376dc8",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 调用麦克风\n",
    "import speech_recognition\n",
    "\n",
    "r = speech_recognition.Recognizer()\n",
    "\n",
    "with speech_recognition.Microphone() as source:\n",
    "    audio = r.listen(source)\n",
    "# 将数据保存到wav文件中\n",
    "with open(\"1.wav\", \"wb\") as f: \n",
    "    f.write(audio.get_wav_data(convert_rate=16000))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "87442dbb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Request time cost 0.828725\n",
      "{\"corpus_no\":\"7098525491988198484\",\"err_msg\":\"success.\",\"err_no\":0,\"result\":[\"哦。\"],\"sn\":\"732055245351652754258\"}\n",
      "\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'{\"corpus_no\":\"7098525491988198484\",\"err_msg\":\"success.\",\"err_no\":0,\"result\":[\"哦。\"],\"sn\":\"732055245351652754258\"}\\n'"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "asr(ru_token,'1.wav')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5a67770a",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
